$(document).ready(function() {
	fillIdPwd();
	$("#studentLoginButton").click(function(event) {
		$.post("studentLogin", {
			email : $("#studentEmailInput").val(),
			password : $("#studentPasswordInput").val()
		}, function(data) {
			if (data.success) {
				rememberStudent();
				window.location.href = "studentHome";
			} else {
				alert(data.errorMessage);
				$("#studentEmailInput")[0].focus();
			}
		}, "json");
	});
	$("#hfLoginButton").click(function(event) {
		$.post("hfLogin", {
			email : $("#hfEmailInput").val(),
			password : $("#hfPasswordInput").val()
		}, function(data) {
			if (data.success) {
				rememberHF();
				window.location.href = "hfHome";
			} else {
				alert(data.errorMessage);
				$("#hfEmailInput")[0].focus();
			}
		}, "json");
	});
	$("#studentLostPassword").click(function(event){
		if("" == $("#studentEmailInput").val()){
			alert("Enter your email in the email input box and then click this link again please.");
			$("#hfEmailInput")[0].focus();
		}else{
			$.post("forgetPassword", {
				email : $("#studentEmailInput").val(),
				userType : 'student'
			}, function(data) {
				if (data.success) {
					alert("Your password was sent to your email, please changed it after you login.");
				} else {
					alert(data.errorMessage);
					$("#studentEmailInput")[0].focus();
				}
			}, "json");
		}
		
	});
	$("#hfLostPassword").click(function(event){
		if("" == $("#hfEmailInput").val()){
			alert("Enter your email in the email input box and then click this link again please.");
			$("#hfEmailInput")[0].focus();
		}else{
			$.post("forgetPassword", {
				email : $("#hfEmailInput").val(),
				userType : 'hf'
			}, function(data) {
				if (data.success) {
					alert("Your password was sent to your email, please changed it after you login.");
				} else {
					alert(data.errorMessage);
					$("#hfEmailInput")[0].focus();
				}
			}, "json");
		}
		
	});
});

function fillIdPwd() {
	try {
		if ($.cookie("TOPPITCH_STUDENT_EMAIL")) {
			$("#studentEmailInput").val($.cookie("TOPPITCH_STUDENT_EMAIL"));
		}
		if ($.cookie("TOPPITCH_STUDENT_PWD")) {
			$("#studentPasswordInput").val($.cookie("TOPPITCH_STUDENT_PWD"));
		}
		if ($.cookie("TOPPITCH_STUDENT_REMEMBER")) {
			$("#rememberMeStudent").attr("checked",
					$.cookie("TOPPITCH_STUDENT_REMEMBER"));
		}
		if ($.cookie("TOPPITCH_HF_EMAIL")) {
			$("#hfEmailInput").val($.cookie("TOPPITCH_HF_EMAIL"));
		}
		if ($.cookie("TOPPITCH_HF_PWD")) {
			$("#hfPasswordInput").val($.cookie("TOPPITCH_HF_PWD"));
		}
		if ($.cookie("TOPPITCH_HF_REMEMBER")) {
			$("#rememberMeHF")
					.attr("checked", $.cookie("TOPPITCH_HF_REMEMBER"));
		}
	} catch (ex) {
	}
}

function rememberStudent() {
	try {
		if ($("#rememberMeStudent").attr("checked") == "checked") {
			$.cookie("TOPPITCH_STUDENT_EMAIL", $("#studentEmailInput").val(), {
				expires : 30
			});
			$.cookie("TOPPITCH_STUDENT_PWD", $("#studentPasswordInput").val(),
					{
						expires : 30
					});
			$.cookie("TOPPITCH_STUDENT_REMEMBER", "checked", {
				expires : 30
			});
		} else {
			$.cookie("TOPPITCH_STUDENT_EMAIL", null);
			$.cookie("TOPPITCH_STUDENT_PWD", null);
			$.cookie("TOPPITCH_STUDENT_REMEMBER", null);
		}
	} catch (ex) {
	}
}

function rememberHF() {
	try {
		if ($("#rememberMeHF").attr("checked") == "checked") {
			$.cookie("TOPPITCH_HF_EMAIL", $("#hfEmailInput").val(), {
				expires : 30
			});
			$.cookie("TOPPITCH_HF_PWD", $("#hfPasswordInput").val(), {
				expires : 30
			});
			$.cookie("TOPPITCH_HF_REMEMBER", "checked", {
				expires : 30
			});
		} else {
			$.cookie("TOPPITCH_HF_EMAIL", null);
			$.cookie("TOPPITCH_HF_PWD", null);
			$.cookie("TOPPITCH_HF_REMEMBER", null);
		}
	} catch (ex) {
	}
}